package com.topscomm.meet.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.topscomm.basic.exception.BusinessException;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.sys.pojo.SysApiLogEntity;
import com.topscomm.sys.service.ISysApiLogService;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;

@Component
@Scope(value = "singleton")
public class MeetServiceApi {
	@Value("${meet.url.mulitSubmitWorkSchedule}")
	public String mulitSubmitWorkScheduleUrl;
	@Value("${meet.url.cancelWorkSchedule}")
	public String cancelWorkSchedule;
	@Autowired
	ISysApiLogService sysApiLogService;

	/**
	 * @description: 批量保存日程
	 * @param
	 * 数据格式：
	 * {
	 * "datas":[{
	 * 'name': '',
	 * 'begintime':'',
	 * 'handleusercode':'',
	 * 'handleusername':'',
	 * ...日程属性,
	 * selectUsers:{
	 * 'usercodes': '',
	 * 'usernames': '',
	 * 'deptcodes': '',
	 * 'deptnames': ''
	 * },
	 * remindList:[{},{}]
	 * }]
	 * }
	 * @author: maxiaodong
	 * @date: 2021年1月28日下午7:50:47
	 * @modify:
	 */
	public String mulitSubmitWorkSchedule(Map<String, Object> requestBody) {
		try {
			String bodyJson = JSONObject.toJSONString(requestBody);
			HttpResponse response = HttpRequest.post(mulitSubmitWorkScheduleUrl).header("Accept", "*/*", false).header("Content-Type", "application/json").body(bodyJson).execute();
			int status = response.getStatus();
			// 添加api调用日志
			SysApiLogEntity logEntity = new SysApiLogEntity();
			logEntity.setPostparam(bodyJson);
			logEntity.setResponsemsg(response.body().toString());
			logEntity.setStatuscode(ConvertUtil.convertToString(response.getStatus()));
			logEntity.setProject("meet");
			List<Map<String, Object>> datasMap = (List<Map<String, Object>>) requestBody.get("datas");
			String sourceid = ConvertUtil.convertToString(datasMap.get(0).get("sourceid"));
			String sourcetype = ConvertUtil.convertToString(datasMap.get(0).get("sourcetype"));
			String nodeInsId = ConvertUtil.convertToString(datasMap.get(0).get("nodeInsId"));
			logEntity.setSourceid(sourceid);
			logEntity.setSourcetype(sourcetype);
			logEntity.setExtendfield1(nodeInsId);
			logEntity.setUrl(mulitSubmitWorkScheduleUrl);
			sysApiLogService.insert(logEntity);
			if (status == HttpStatus.HTTP_OK) {
				return response.body();
			} else {
				throw new BusinessException("接口请求失败：状态码(" + status + ")，" + response.body());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("接口请求异常：" + e.getMessage());
		}
	}

	/**
	 * @description: 撤销日程
	 * @param
	 * 数据格式：
	 * {
	 * "sourceapp": "",
	 * "sourcetype": "",
	 * "sourceids": ""
	 * }
	 * @author: maxiaodong
	 * @date: 2021年2月1日下午10:05:47
	 * @modify:
	 */
	public String cancelWorkSchedule(Map<String, Object> requestBody) {
		try {
			String bodyJson = JSONObject.toJSONString(requestBody);
			HttpResponse response = HttpRequest.post(cancelWorkSchedule).header("Accept", "*/*", false).header("Content-Type", "application/json").body(bodyJson).execute();
			int status = response.getStatus();
			if (status == HttpStatus.HTTP_OK) {
				return response.body();
			} else {
				throw new BusinessException("接口请求失败：状态码(" + status + ")，" + response.body());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("接口请求异常：" + e.getMessage());
		}
	}

	/**
	 * @description: 删除日程
	 * @param sourceapp
	 * @param sourcetype
	 * @param sourceids
	 * @author: maxiaodong
	 * @date: 2021年3月4日下午1:54:58
	 * @modify:
	 */
	public void cancelWorkSchedule(String sourceapp, String sourcetype, String sourceids) {
		Map<String, Object> paraMap = new HashMap<>();
		paraMap.put("sourceapp", sourceapp);
		paraMap.put("sourcetype", sourcetype);
		paraMap.put("sourceids", sourceids);
		this.cancelWorkSchedule(paraMap);
		// 删除日程时删除历史记录
		StringBufferProxy deleteSql = new StringBufferProxy();
		deleteSql.appendSingle("project in ('{0}') and sourcetype in ('{1}') and sourceid in ('{2}') and url like '%{3}'", "meet", sourcetype, sourceids, "mulitSubmitWorkSchedule");
		sysApiLogService.deleteByWhere(deleteSql.toString());
	}
}
